// Copyright (C) 1997-2002 Alias|Wavefront,
// a division of Silicon Graphics Limited.
//
// The information in this file is provided for the exclusive use of the
// licensees of Alias|Wavefront.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias|Wavefront license agreement, without fee.
//
// ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
//
//
// Script:    textureWindowCreateToolBar.mel
//
//*AUTOMATIC: yes
//
//
// SYNOPSIS
//		Creates a toolbar for the panel that contains the texture window
//

global proc int textureWindowCreateToolBar_isUVTransformed()
//
// Description:
//	this procedure updates the fields if the UV is transformed or
//	if a new UV is selected. If more than 1 UV is selected, the lowest 
//	indexed UV's coordinates are displayed

{
	int $return = 0;
	string $UVs[] = `filterExpand -selectionMask 35`;
	global float $gUVvalues[];
	global float $gPreviousUVvalues[];

	//	is the texture editor visible?
	int $texturePanelVisible = 0;
	string $visiblePanels[] = `getPanel -visiblePanels`;
	for ($panel in $visiblePanels){
		if ($panel == "polyTexturePlacementPanel1") $texturePanelVisible = 1;
	}

	if ($texturePanelVisible){
		if ((size($UVs)) > 0){
			$gUVvalues = `polyEditUV -query $UVs[0]`;

			if (!`size ($gPreviousUVvalues)`){
				$gPreviousUVvalues[0] = $gUVvalues[0];
				$gPreviousUVvalues[1] = $gUVvalues[1];
			}
			if ($gPreviousUVvalues[0] != $gUVvalues[0]){
				floatField -edit
					-value $gUVvalues[0]
					uvEntryFieldU;		
				$gPreviousUVvalues[0] = $gUVvalues[0];
				$return = 1;
			}
			if ($gPreviousUVvalues[1] != $gUVvalues[1]){
				floatField -edit
					-value $gUVvalues[1]
					uvEntryFieldV;		
				$gPreviousUVvalues[1] = $gUVvalues[1];
				$return = 1;
			}
		} else {
			//	need to initiate values if they've not been set yet
			$gUVvalues[0] = 0.0001; $gUVvalues[1] = 0.0001;
			$gPreviousUVvalues[0] = 0.0002; $gPreviousUVvalues[1] = 0.0002;
		}
	}
	return $return;
}

global proc textureWindowCreateToolBar_uvCopy()
//
// Description:
//	this procedure defines the copy behaviour for UV mode
//
{
	global float $gUVcopyValues[];
	$gUVcopyValues = `polyEditUV -q`;
}

global proc textureWindowCreateToolBar_uvPaste( int $u, int $v)
//
// Description:
//	this procedure defines the paste behaviour for UV mode
//		$u: boolean - paste U value
//		$v: boolean - paste V value
//
{
	global float $gUVcopyValues[];
	string $UVs[] = `ls -selection -flatten`;
	for ($UV in $UVs){
		if ($u == 1){
			polyEditUV -r false -u $gUVcopyValues[0];
		}
		if ($v == 1){
			polyEditUV -r false -v $gUVcopyValues[1];
		}
	}
}


global proc textureWindowCreateToolBar_copyPasteMode (int $mode)
//
// Description:
//	this procedure updates the copy paste mode for the 
//	toolbar
//		0: off - copy faces
//		1: on  - copy UVs
//
{
	int $iconSize = 30;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	switch ($mode){
	case 0:
		iconTextButton -edit
			-command "PolygonCopy"
			-annotation (getRunTimeCommandAnnotation ("PolygonCopy"))
			copyUVButton;

		popupMenu -button 3
			-parent copyUVButton
			-postMenuCommand "PolygonCopyOptions"
			copyUVButtonPopup;

		iconTextButton -edit
			-command "PolygonPaste"
			-annotation "Paste UVs to selected faces"
			pasteUVButton;

		popupMenu -button 3
			-parent pasteUVButton
			-postMenuCommand "PolygonPasteOptions"
			pasteUVButtonPopup;

		iconTextButton -edit 
			-enable false 
			//-image1 "pasteUDisabled.xpm"
			pasteUButton;

		iconTextButton -edit 
			-enable false 
			//-image1 "pasteVDisabled.xpm"
			pasteVButton;

		break;

	case 1:
		//	adjust copy/paste buttons
		iconTextButton -edit
			-command "textureWindowCreateToolBar_uvCopy"
			-annotation "Copy UV values of selected UV"
			copyUVButton;

		iconTextButton -edit
			-command "textureWindowCreateToolBar_uvPaste 1 1"
			-annotation "Paste UV values to selected UVs"
			pasteUVButton;

		//	delete their popup menus
		deleteUI -menu copyUVButtonPopup;
		deleteUI -menu pasteUVButtonPopup;

		//	enable paste U and V buttons
		iconTextButton -edit 
			-enable true 
			//-image1 "pasteU.xpm"
			pasteUButton;

		iconTextButton -edit 
			-enable true 
			//-image1 "pasteV.xpm"
			pasteVButton;

		break;
	}

	setUITemplate -popTemplate;

}


global proc textureWindowCreateToolBar_toggleIcons ( int $arg, 
													 string $iconLayout, 
													 string $collapsedLayout, 
													 string $shiftedLayout, 
													 string $statusVariable )
//
// Description:
//  Show and Hide the file icons
// Parameters:
//  $arg: 1 means show, 0 means hide, -1 means use optionVar
//  $iconLayout: the name of the layout being toggled
//  $collapsedLayout: the name of the layout/icon containing the open/close bar
//  $shiftedLayout: The name of the layout neighboring iconLayout to the right.
//                  This layout will be shifted over when iconLayout is hidden.
//					If no righthand layout exists, this parameter should be any
//					invalid layout name (ex: "").
//  $statusVariable: the optionVar storing whether or not $iconLayout is hidden
//					 or shown.
//
{	
	// UV Texture Editor Toolbar (the parent of all icon layouts)
	//
	global string $gUVTexEditToolBar;
	setParent $gUVTexEditToolBar;

	int $state = $arg;
	if ($state < 0) $state = (!`optionVar -q $statusVariable`);

	// Determine whether or not the specified $shiftedLayout exists
	//
	int $doesShiftedExist = false;
	string $children[] = `formLayout -q -childArray $gUVTexEditToolBar`;
	int $i = 0;
	for ($i = 0; $i < `formLayout -q -numberOfChildren $gUVTexEditToolBar`; $i++ ) {
		if ($children[$i] == $shiftedLayout) {
			$doesShiftedExist = true;
			break;
		}
	}

	// number of pixels between neighboring layouts
	//
	int $edging = 2;
	if ($state) {
		// Show $iconLayout
		//

		// Because the toolbar contains both gridLayouts and formLayouts,
		// we need to check which one is being toggled
		//
		if ( `gridLayout -exists $iconLayout`) {
			gridLayout -edit -manage true $iconLayout;
		} else if ( `formLayout -exists $iconLayout` ) {
			formLayout -edit -manage true $iconLayout;
		}
		
		// Shift right hand neighbor back to the right
		//
		if ($doesShiftedExist) {
			formLayout -e 
				-ac $shiftedLayout left $edging $iconLayout 
				$gUVTexEditToolBar;
		}

		iconTextButton -edit -i1 textureEditorOpenBar.xpm $collapsedLayout;
	} else {
		// Hide $iconLayout
		//

		// Because the toolbar contains both gridLayouts and formLayouts,
		// we need to check which one is being toggled
		//
		if ( `gridLayout -exists $iconLayout`) {
			gridLayout -edit -manage false $iconLayout;
		} else if ( `formLayout -exists $iconLayout` ) {
			formLayout -edit -manage false $iconLayout;
		}

		// Shift right hand neighbor over to the left
		//
		if ($doesShiftedExist) {
			formLayout -e 
				-ac $shiftedLayout left $edging $collapsedLayout 
				$gUVTexEditToolBar;
		}

		iconTextButton -edit -i1 textureEditorCloseBar.xpm $collapsedLayout;
	}

	// the status variable should now represent the current state
	// (shown: 1, hidden: 0)
	//
	optionVar -intValue $statusVariable $state;
}

global proc textureWindowCreateToolBar(string $toolBar,
									   string $editor,
									   string $editorCmd)
//
// Description:
//	Add the texture window toolbar
//
//  A call to txtWndUpdateEditor has been added to several of the
//	buttons. This is to keep those buttons (which toggle between two
//	states) and the equivalent menuItems in sync.
// 
// Returns: name of toolbar widget
//
{
	// So the icon toggle procedures know who their parent is
	global string $gUVTexEditToolBar;
	$gUVTexEditToolBar = $toolBar;
	
	// Space between icons
	int $vertGap = 5;
	int $horizGap = 2;

	// Add in the icons
	int $iconSize = 30;
	int $floatWidth = 30;

	// Open/Close bar size
	int $barHeight = 2 * $iconSize + $vertGap;
	int $barWidth = 9;

	// Template for the quick collapse bar, and the hide/show bars
	//
    if (`uiTemplate -exists TexWinBarTemplate`) {
    	deleteUI -uiTemplate TexWinBarTemplate;
    }
    uiTemplate TexWinBarTemplate;
	iconTextButton -defineTemplate TexWinBarTemplate
		-width $barWidth -height $barHeight;

	// Template for all the toolbar push buttons
	// 
	if (`uiTemplate -exists TexWinButtonTemplate`) {
    	deleteUI -uiTemplate TexWinButtonTemplate;
    }
    uiTemplate TexWinButtonTemplate;
	iconTextButton -defineTemplate TexWinButtonTemplate
		-width $iconSize -height $iconSize;

	// Template for all the toolbar toggle buttons
	//
    if (`uiTemplate -exists TexWinCheckBoxTemplate`) {
    	deleteUI -uiTemplate TexWinCheckBoxTemplate;
    }
    uiTemplate TexWinCheckBoxTemplate;
	iconTextCheckBox -defineTemplate TexWinCheckBoxTemplate
		-width $iconSize -height $iconSize;

	// Template for the toolbar float fields
	//
    if (`uiTemplate -exists TexWinFloatFieldTemplate`) {
    	deleteUI -uiTemplate TexWinFloatFieldTemplate;
    }
    uiTemplate TexWinFloatFieldTemplate;
	iconTextCheckBox -defineTemplate TexWinFloatFieldTemplate
		-width $floatWidth -height $iconSize;
	
	//////////////////
	// 	Flip/Rotate //
	//////////////////
		
	
	// 
	// Init hidden/shown option var
	//
	if (!`optionVar -exists "showStatusFlipRotate"`) {
		optionVar -intValue "showStatusFlipRotate" 1;
	}

	//
	// Set up open/close bar
	//
	setUITemplate -pushTemplate TexWinBarTemplate;

	iconTextButton -vis true
		-parent $gUVTexEditToolBar
		-ann "Show/hide the flip/rotate icons"
		-i1 textureEditorOpenBar.xpm  
		-c ("textureWindowCreateToolBar_toggleIcons(-1," +
			"\"flipRotateLayout\", " +
			"\"flipRotateCollapse\", " + 
			"\"moveSewCollapse\", " + 
			"\"showStatusFlipRotate\")")
		flipRotateCollapse;

	setUITemplate -popTemplate;

	//
	// Set up icons
	//
	formLayout flipRotateLayout;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	iconTextButton
		-image1 "flipU.xpm"
		//	note polyForceUVs may be phased out - it's marked obsolete
		//	but it converts the selection and doesn't mess up tri-strips
		//	like polyFlipUV (if you change, don't forget flipV below)
		//-command "ConvertSelectionToFaces; polyFlipUV -flipType 0 -local on"
		-command "polyForceUV -flipHorizontal -local"
		-annotation "Flip UVs: Flip selected UVs in U direction"
		flipUButton;

	iconTextButton
		-image1 "flipV.xpm"
		//-command "ConvertSelectionToFaces; polyFlipUV -flipType 1 -local on"
		-command "polyForceUV -flipVertical -local"
		-annotation "Flip UVs: Flip selected UVs in V direction"
		flipVButton;

	iconTextButton
		-image1 "rotateUVcw.xpm"
		-command ("string $selection[] = `ls -sl`;" +
				  "ConvertSelectionToUVs; polyRotateUVs -45;" +
				  "select -replace $selection")
		-annotation "Rotate UVs: Rotate selected UVs clockwise"
		rotateCWButton;

	iconTextButton
		-image1 "rotateUVccw.xpm"
		-command ("string $selection[] = `ls -sl`;" +
				  "ConvertSelectionToUVs; polyRotateUVs 45;" +
				  "select -replace $selection")
		-annotation "Rotate UVs: Rotate selected UVs counter clockwise"
		rotateCCWButton;

	setUITemplate -popTemplate;

	//
	// Position icons
	//
	formLayout -edit
		-attachForm     flipUButton		    "top"   0
		-attachForm     flipUButton		    "left"  0

		-attachControl  flipVButton		    "left"	2 flipUButton
		-attachForm     flipVButton			"top"	0

		-attachForm     rotateCCWButton		"left"  0
		-attachControl  rotateCCWButton		"top"	2 flipUButton
		-attachControl  rotateCWButton		"left"  2 rotateCCWButton
		-attachControl  rotateCWButton		"top"	2 flipVButton

		flipRotateLayout;

	//////////////
	// Move/Sew //
	//////////////


	setParent ..;

	//
	// Init hidden/shown option var
	//
	if (!`optionVar -exists "showStatusMoveSew"`) {
		optionVar -intValue "showStatusMoveSew" 1;
	}

	//
	// Set up open/close bar
	//
	setUITemplate -pushTemplate TexWinBarTemplate;

	iconTextButton -vis true
		-parent $gUVTexEditToolBar
		-ann "Show/hide the move/sew icons"
		-i1 textureEditorOpenBar.xpm  
		-c ("textureWindowCreateToolBar_toggleIcons(-1, " + 
			"\"moveSewLayout\", " + 
			"\"moveSewCollapse\", " + 
			"\"alignCollapse\", " + 
			"\"showStatusMoveSew\")")
		moveSewCollapse;

	setUITemplate -popTemplate;

	//
	// Set up icons
	//
	gridLayout -numberOfRowsColumns 2 3 moveSewLayout;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	iconTextButton
		-image1 "cutUV.xpm"
		-command "polyMapCut"
		-annotation "Cut UVs: Separate the UVs along the selected edges"
		cutButton;

	iconTextButton
		-image1 "sewUV.xpm"
		// merge UVs if not edges
		-command ("string $edges[] = `filterExpand -selectionMask 32`;" +
			"string $uvs[] = `filterExpand -selectionMask 35`;" +
			"if (size($edges) > 0) polyMapSew;" +
			"if (size($uvs) > 0) performPolyMergeUV 0;")
		-annotation "Sew UVs: Sew the selected edges or UVs together"
		sewButton;

	popupMenu -button 3
		-parent sewButton
		-postMenuCommand "performPolyMergeUV 1"
		sewButtonPopup;

	iconTextButton
		-image1 "layoutUV.xpm"
		-command "ConvertSelectionToFaces; performPolyLayoutUV 0"
		-annotation "Layout UVs: Select faces to be moved in UV space"
		layoutButton;

	popupMenu -button 3
		-parent layoutButton
		-postMenuCommand "performPolyLayoutUV 1"
		layoutButtonPopup;

	iconTextButton
		-image1 "moveSewUV.xpm"
		-command "performPolyMapSewMove 0"
		-annotation "Move and Sew UVs: Move and sew the the selected edges"
		moveSewButton;

	popupMenu -button 3
		-parent moveSewButton
		-postMenuCommand "performPolyMapSewMove 1"
		moveSewButtonPopup;

	iconTextButton 
		-image1 "splitUV.xpm" 
		-annotation "Split Selected UV: Separate the selected UV into one for each connected edge"
		-command "polySplitTextureUV"
		splitUVButton;

	iconTextButton
			-image1 "cycleUVs.xpm"
			-command "polyRotateUVsByVertex"
			-annotation "Cycle UVs: Cycles the UVs of the selected face counter clockwise."
			cycleUVsButton;

	setUITemplate -popTemplate;


	///////////
	// Align //
	///////////
	setParent ..;


	//
	// Init hidden/shown option var
	//
	if (!`optionVar -exists "showStatusAlign"`) {
		optionVar -intValue "showStatusAlign" 1;
	}

	//
	// Set up open/close bar
	//
	setUITemplate -pushTemplate TexWinBarTemplate;

	iconTextButton -vis true
		-parent $gUVTexEditToolBar
		-ann "Show/hide the flip/rotate icons"
		-i1 textureEditorOpenBar.xpm  
		-c ("textureWindowCreateToolBar_toggleIcons(-1," +
			"\"alignLayout\", " + 
			"\"alignCollapse\", " + 
			"\"isolateSelectCollapse\", " + 
			"\"showStatusAlign\")")
		alignCollapse;

	setUITemplate -popTemplate;

	//
	// Set up icons
	//
	gridLayout -numberOfRowsColumns 2 3 alignLayout;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	// align tools
	iconTextButton
		-image1 "alignUMin.xpm"
		-command "alignUV 1 1 0 0"
		-annotation "Align UVs: Align selected UVs to minimum U value"
		alignUMinButton;

	iconTextButton
		-image1 "alignUMax.xpm"
		-command "alignUV 1 0 0 0"
		-annotation "Align UVs: Align selected UVs to maximum U value"
		alignUMaxButton;
		
	iconTextButton
		-image1 "alignVMin.xpm"
		-command "alignUV 0 0 1 1"
		-annotation "Align UVs: Align selected UVs to minimum V value"
		alignVMinButton;
		
	iconTextButton
		-image1 "alignVMax.xpm"
		-command "alignUV 0 0 1 0"
		-annotation "Align UVs: Align selected UVs to maximum V value"
		alignVMaxButton;

	iconTextButton
		-image1 "polyGridUV.xpm"
		-command "GridUV"
		-annotation "Grid UVs: Snap selected UVs to user specified grid"
		gridUVButton;

	popupMenu -button 3
		-parent gridUVButton
		-postMenuCommand "GridUVOptions"
		gridUVPopup;

	iconTextButton
		-image1 "relaxUV.xpm"
		-command "performPolyUntangleUV relax 0"
		-annotation "Relax UVs: Automatically move UVs for better texture space distribution"
		relaxButton;

	popupMenu -button 3
		-parent relaxButton
		-postMenuCommand "performPolyUntangleUV relax 1"
		relaxButtonPopup;
	
	setUITemplate -popTemplate;

	////////////////////
	// Isolate/Select //
	////////////////////

	setParent ..;

	//
	// Init hidden/shown option var
	//
	if (!`optionVar -exists "showStatusIsolateSelect"`) {
		optionVar -intValue "showStatusIsolateSelect" 1;
	}

	//
	// Set up open/close bar
	//
	setUITemplate -pushTemplate TexWinBarTemplate;

	iconTextButton -vis true
		-parent $gUVTexEditToolBar
		-ann "Show/hide the isolate/select icons"
		-i1 textureEditorOpenBar.xpm  
		-c ("textureWindowCreateToolBar_toggleIcons(-1," + 
			"\"isolateSelectLayout\", " + 
			"\"isolateSelectCollapse\", " + 
			"\"displayCollapse\", " + 
			"\"showStatusIsolateSelect\")")
		isolateSelectCollapse;

	setUITemplate -popTemplate;

	//
	// Set up icons
	//
	formLayout isolateSelectLayout;

	setUITemplate -pushTemplate TexWinCheckBoxTemplate;

	iconTextCheckBox 
			-image1 "uvIsolateSelect.xpm" 
			-onCommand("textureWindow -e -useFaceGroup 1 " + $editor + "; " +
					   "optionVar -iv textureWindowShaderFacesMode 2;" +
					   "txtWndUpdateEditor(" +
					   "\"" + $editor + "\", " +
					   "\"" + $editorCmd + "\", \"null\", 101);")
			-offCommand ("textureWindow -e -useFaceGroup 0 " + $editor + "; " +
						 "optionVar -iv textureWindowShaderFacesMode 0;" +
						 "txtWndUpdateEditor(" + 
						 "\"" + $editor + "\", " +
						 "\"" + $editorCmd + "\", \"null\", 101);")
			-annotation "Toggle Isolate Select mode"
			-value `optionVar -q textureWindowShaderFacesMode`				
			isolateSelectButton;

	setUITemplate -popTemplate;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	iconTextButton
			-image1 "uvIsolateSelectReset.xpm"
			-command "textureEditorIsolateSelect 0;"
			-annotation "Remove All: Remove all UVs of the selected object from the isolate select set."
			isolateResetButton;

	iconTextButton
			-image1 "uvIsolateSelectAdd.xpm"
			-command "textureEditorIsolateSelect 1;"
			-annotation "Add Selected: Add selected UVs to the isolate select set."
			uvIsolateAddButton;

	iconTextButton
			-image1 "uvIsolateSelectRemove.xpm"
			-command "textureEditorIsolateSelect 2;"
			-annotation "Remove Selected: Remove selected UVs to the isolate select set."
			uvIsolateRemoveButton;

	setUITemplate -popTemplate;

	formLayout -edit
		-attachForm     isolateSelectButton		"top"   0
		-attachForm		isolateSelectButton		"left"  0 
		-attachControl  uvIsolateAddButton		"left"	2 isolateSelectButton
		-attachForm     uvIsolateAddButton		"top"	0

		-attachForm		isolateResetButton		"left"	0 
		-attachControl	isolateResetButton		"top"	2 isolateSelectButton
		-attachControl	uvIsolateRemoveButton	"left"	2 isolateResetButton
		-attachControl	uvIsolateRemoveButton	"top"	2 uvIsolateAddButton
		isolateSelectLayout;

	/////////////
	// Display //
	/////////////

	setParent ..;
	// Display options

	//
	// Init hidden/shown option var
	// 
	if (!`optionVar -exists "showStatusDisplay"`) {
		optionVar -intValue "showStatusDisplay" 1;
	}

	//
	// Set up open/close bar
	//
	setUITemplate -pushTemplate TexWinBarTemplate;

	iconTextButton -vis true
		-parent $gUVTexEditToolBar
		-ann "Show/hide the display icons"
		-i1 textureEditorOpenBar.xpm  
		-c ("textureWindowCreateToolBar_toggleIcons(-1," +
			"\"displayLayout\", " + 
			"\"displayCollapse\", " + 
			"\"copyPasteCollapse\", " + 
			"\"showStatusDisplay\")")
		displayCollapse;

	setUITemplate -popTemplate;

	//
	// Set up icons
	//
	gridLayout -numberOfRowsColumns 2 4 displayLayout;

	setUITemplate -pushTemplate TexWinCheckBoxTemplate;

	iconTextCheckBox 
		-image1 "imageDisplay.xpm" 
		-onCommand("textureWindow -e -imageDisplay 1 " + $editor + "; " +
				   "refresh; " +
				   "txtWndUpdateEditor(" +
				   "\"" + $editor + "\"," + 
				   "\"" + $editorCmd + "\", \"null\", 101);")
		-offCommand ("textureWindow -e -imageDisplay 0 " + $editor + "; " +
					 "refresh; " +
					 "txtWndUpdateEditor(" +
					 "\"" + $editor + "\"," + 
					 "\"" + $editorCmd + "\", \"null\", 101);")
		-annotation "Show background image on/off"
		-value `textureWindow -q -imageDisplay  $editor`
		imageDisplayButton;

	popupMenu -button 3
		-parent imageDisplayButton
		-postMenuCommand "performTextureViewImageRangeOptions 1"
		imageDisplayButtonPopup;
		
	iconTextCheckBox 
		-image1 "gridDisplay.xpm" 
		-onCommand ("textureWindow -e -toggle 1 " + $editor + "; " +
					"txtWndUpdateEditor(" +
					"\"" + $editor + "\"," + 
					"\"" + $editorCmd + "\", \"null\", 101);")
		-offCommand ( "textureWindow -e -toggle 0 " + $editor + "; " +
					  "txtWndUpdateEditor(" +
					  "\"" + $editor + "\"," + 
					  "\"" + $editorCmd + "\", \"null\", 101);")
		-annotation "Show grid on/off"
		-value `textureWindow -q -toggle $editor`
		gridDisplayButton;

	popupMenu -button 3
		-parent gridDisplayButton
		-postMenuCommand "performTextureViewGridOptions 1"
		gridDisplayButtonPopup;

	iconTextCheckBox 
		-image1 "pixelSnap.xpm" 
		-onCommand ("textureWindow -e -imagePixelSnap 1 " + $editor + "; " +
					"txtWndUpdateEditor(" +
					"\"" + $editor + "\"," + 
					"\"" + $editorCmd + "\", \"null\", 101);")
		-offCommand ("textureWindow -e -imagePixelSnap 0 " + $editor + "; " +
					 "txtWndUpdateEditor(" +
					 "\"" + $editor + "\"," + 
					 "\"" + $editorCmd + "\", \"null\", 101);")
		-annotation "Toggle snap to pixels on/off"
		-value `textureWindow -q -imagePixelSnap $editor`
		pixelSnapButton;

	iconTextCheckBox 
		-image1 "filteredMode.xpm" 
		-onCommand ("textureWindow -e -imageUnfiltered 1 " + $editor + "; " +
					"txtWndUpdateEditor(" +
					"\"" + $editor + "\"," + 
					"\"" + $editorCmd + "\", \"null\", 101);")
		-offCommand ( "textureWindow -e -imageUnfiltered 0 " + $editor + "; " +
					  "txtWndUpdateEditor(" +
					  "\"" + $editor + "\"," + 
					  "\"" + $editorCmd + "\", \"null\", 101);")
		-annotation "Toggle filtered display mode on/off"
		-value `textureWindow -q -imageUnfiltered $editor`
		filteredButton;

	iconTextCheckBox 
		-image1 "imageRatio.xpm" 
		-onCommand ("textureWindow -e -imageRatio 1 " + $editor + "; " +
					"txtWndUpdateEditor(" +
					"\"" + $editor + "\"," + 
					"\"" + $editorCmd + "\", \"null\", 101);")
		-offCommand ( "textureWindow -e -imageRatio 0 " + $editor + "; " +
					  "txtWndUpdateEditor(" +
					  "\"" + $editor + "\"," + 
					  "\"" + $editorCmd + "\", \"null\", 101);")
		-annotation "Toggle image ratio on/off"
		-value `textureWindow -q -imageRatio $editor`
		ratioButton;

	setUITemplate -popTemplate;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	iconTextButton
		-image1 "textureBorder.xpm"
		-command ("int $borders[] = `polyOptions -q -displayMapBorder`;" +
				  "float $borderWidth[] = `optionVar -q displayPolyBorderEdgeSize`;" +
				  "polyOptions -displayMapBorder (!$borders[0]) -sizeBorder $borderWidth[1]")
		-annotation "Toggle Texture Borders: Toggle the display of texture borders for the active mesh"
		polyDisplayButton;

	popupMenu -button 3
		-parent polyDisplayButton
		-postMenuCommand "CustomPolygonDisplayOptions"
		polyDisplayPopup;

	iconTextButton
		-image1 "rvAllPlanes.xpm"
		-annotation "Display RGB Channels"
		-command
		("textureWindow -e -displayStyle \"color\" " + $editor)
		colorButton;

	iconTextButton
		-image1 "rvMaskPlane.xpm"
		-annotation "Display Alpha Channel"
		-command 				
		("textureWindow -e -displayStyle \"mask\" " + $editor)
		maskButton;	

	setUITemplate -popTemplate;

	////////////////
	// Copy/Paste //
	////////////////
	setParent ..;

	//
	// Init hidden/shown option var
	//
	if (!`optionVar -exists "showStatusCopyPaste"`) {
		optionVar -intValue "showStatusCopyPaste" 1;
	}

	//
	// Set up open/close bar
	//
	setUITemplate -pushTemplate TexWinBarTemplate;

	iconTextButton -vis true
		-parent $gUVTexEditToolBar
		-ann "Show/hide the copy/paste icons"
		-i1 textureEditorOpenBar.xpm  
		-c ("textureWindowCreateToolBar_toggleIcons(-1," +
			"\"copyPasteLayout\", " +
			"\"copyPasteCollapse\", " +
			"\"\", " + 
			"\"showStatusCopyPaste\")")
		copyPasteCollapse;

	setUITemplate -popTemplate;

	//
	// Set up icons
	//
	// copy/paste controls
	formLayout copyPasteLayout;

	// UV Entry field. Will allow user to set current uv value
	// of uv in selection list. Will update when selection list
	// changes. 
	//
	// Ideally the fields should update as the selected UV(s)
	// is/are transformed but there is not a good way to do
	// this. Should be an event that exists for scriptJob.

	setUITemplate -pushTemplate TexWinFloatFieldTemplate;

	floatField 
		-precision 3
		-ed true
		-enterCommand "polyEditUV -r false -u `floatField -q -value uvEntryFieldU`"
		-cc "polyEditUV -r false -u `floatField -q -value uvEntryFieldU`"
		-ann "Enter value to set U value to"
		uvEntryFieldU;

	floatField 
		-precision 3
		-ed true
		-enterCommand "polyEditUV -r false -v `floatField -q -value uvEntryFieldV`"
		-cc "polyEditUV -r false -v `floatField -q -value uvEntryFieldV`"
		-ann "Enter value to set V value to"
		uvEntryFieldV;

	setUITemplate -popTemplate;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	// originally planned to lock current UV values but it's
	// more important to have an update button for the selected
	// UV's coordinates
	iconTextButton 
		-image1 "uvUpdate.xpm" 
		-annotation "Refresh the current UV values"
		-command "textureWindowCreateToolBar_isUVTransformed"
		updateValueButton;

	iconTextButton
		-image1 "copyUV.xpm"
		-command "PolygonCopy"
		-annotation (getRunTimeCommandAnnotation ("PolygonCopy"))
		copyUVButton;

	popupMenu -button 3
		-parent copyUVButton
		-postMenuCommand "PolygonCopyOptions"
		copyUVButtonPopup;

	iconTextButton
		-image1 "pasteUV.xpm"
		-command "PolygonPaste"
		-annotation (getRunTimeCommandAnnotation ("PolygonPaste"))
		pasteUVButton;

	popupMenu -button 3
		-parent pasteUVButton
		-postMenuCommand "PolygonPasteOptions"
		pasteUVButtonPopup;

	setUITemplate -popTemplate;

	setUITemplate -pushTemplate TexWinCheckBoxTemplate;

	iconTextCheckBox 
		-image1 "copyUVMode.xpm" 
		-onCommand		"textureWindowCreateToolBar_copyPasteMode 1"
		-offCommand		"textureWindowCreateToolBar_copyPasteMode 0"
		-annotation "Toggle copy/paste for faces/UVs"
		copyModeButton;

	setUITemplate -popTemplate;

	setUITemplate -pushTemplate TexWinButtonTemplate;

	iconTextButton
		-enable false
		-image1 "pasteU.xpm"
		-command "textureWindowCreateToolBar_uvPaste 1 0"
		-annotation "Paste U value to selected UVs"
		pasteUButton;

	iconTextButton
		-enable false
		-image1 "pasteV.xpm"
		-width $iconSize -height $iconSize
		-command "textureWindowCreateToolBar_uvPaste 0 1"
		-annotation "Paste V value to selected UVs"
		pasteVButton;

	setUITemplate -popTemplate;

	formLayout -edit
	
		-attachForm     uvEntryFieldU		"top"	$vertGap
		-attachForm		uvEntryFieldU		"left"	0 
		-attachForm     uvEntryFieldV		"top"	$vertGap
		-attachControl  uvEntryFieldV		"left"	$horizGap uvEntryFieldU
		-attachForm     updateValueButton	"top"	0
		-attachControl  updateValueButton	"left"	$horizGap uvEntryFieldV

		-attachControl	copyUVButton		"top"	$vertGap uvEntryFieldU
		-attachForm		copyUVButton		"left"	0
		-attachControl  pasteUVButton		"top"	$vertGap uvEntryFieldU
		-attachControl  pasteUVButton		"left"	$horizGap copyUVButton
		-attachControl  pasteUButton		"top"	$vertGap uvEntryFieldU
		-attachControl  pasteUButton		"left"	$horizGap pasteUVButton
		-attachControl  pasteVButton		"top"	$vertGap uvEntryFieldU
		-attachControl  pasteVButton		"left"	$horizGap pasteUButton
		-attachControl  copyModeButton		"top"	$vertGap uvEntryFieldU
		-attachControl  copyModeButton		"left"	$horizGap pasteVButton
		copyPasteLayout;		

	///////////////////////////////
	// UV Texture Editor ToolBar //
	///////////////////////////////

	setParent ..;


	// build form
	formLayout -edit  

		-attachForm		flipRotateCollapse	"top"	$vertGap
		-attachForm		flipRotateCollapse	"left"	0

		-attachForm     flipRotateLayout	"top"   $vertGap
		-attachControl	flipRotateLayout	"left"  2 flipRotateCollapse

		-attachForm     moveSewCollapse		"top"   $vertGap
		-attachControl	moveSewCollapse		"left"  2 flipRotateLayout

		-attachForm     moveSewLayout		"top"   $vertGap
		-attachControl	moveSewLayout		"left"	2 moveSewCollapse

		-attachForm     alignCollapse		"top"   $vertGap
		-attachControl	alignCollapse		"left"	2 moveSewLayout

		-attachForm     alignLayout			"top"   $vertGap
		-attachControl	alignLayout			"left"	2 alignCollapse

		-attachForm     isolateSelectCollapse		"top"   $vertGap
		-attachControl	isolateSelectCollapse		"left"	2 alignLayout

		-attachForm     isolateSelectLayout		"top"   $vertGap
		-attachControl	isolateSelectLayout		"left"	2 isolateSelectCollapse

		-attachForm     displayCollapse		"top"   $vertGap
		-attachControl	displayCollapse		"left"	2 isolateSelectLayout

		-attachForm     displayLayout		"top"   $vertGap
		-attachControl	displayLayout		"left"	2 displayCollapse

		-attachForm     copyPasteCollapse	"top"   $vertGap
		-attachControl	copyPasteCollapse	"left"	2 displayLayout

		-attachForm     copyPasteLayout		"top"   $vertGap
		-attachControl	copyPasteLayout		"left"	2 copyPasteCollapse
	
		$toolBar;

	// scriptJob to update the UV fields
	scriptJob -parent $editor
		-event "SelectionChanged"
		"textureWindowCreateToolBar_isUVTransformed";
}
